package com.example.project_01.service.impl;

import com.example.project_01.commons.Result;
import com.example.project_01.dto.MedicalPolicyQueryDTO;
import com.example.project_01.mapper.MedicalPolicyMapper;
import com.example.project_01.model.MedicalPolicyModel;
import com.example.project_01.pojo.MedicalPolicy;
import com.example.project_01.service.MedicalPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MedicalPolicyServiceImpl implements MedicalPolicyService {

    @Autowired
    private MedicalPolicyMapper medicalPolicyMapper;

    @Override
    public List<MedicalPolicyModel> getPolicyList(MedicalPolicyQueryDTO queryDTO) {
        return medicalPolicyMapper.selectByCondition(queryDTO);
    }

    @Override
    public Result getPolicyPage(MedicalPolicyQueryDTO queryDTO) {
        // 计算分页偏移量
        int offset = (queryDTO.getPageNum() - 1) * queryDTO.getPageSize();

        // 查询分页数据
        List<MedicalPolicyModel> policyList = medicalPolicyMapper.selectByPage(
                queryDTO,
                offset,
                queryDTO.getPageSize()
        );

        // 查询总条数
        Integer totalCount = medicalPolicyMapper.selectTotalCount(queryDTO);

        // 封装分页结果
        Map<String, Object> pageResult = new HashMap<>(2);
        pageResult.put("list", policyList);
        pageResult.put("total", totalCount);

        return new Result().setCode(200).setMessage("查询成功").setData(pageResult);
    }

    @Override
    public MedicalPolicyModel getPolicyById(Long id) {
        MedicalPolicyQueryDTO queryDTO = new MedicalPolicyQueryDTO();
        // 关键修正：设置ID条件，确保查询指定ID的政策
        queryDTO.setId(id);
        // 调用Mapper的条件查询方法
        List<MedicalPolicyModel> policyList = medicalPolicyMapper.selectByCondition(queryDTO);
        // 若查询结果为空，返回null；否则返回第一条（ID唯一）
        return policyList.isEmpty() ? null : policyList.get(0);
    }

    /*添加医保政策*/
    @Override
    public Result addMedicalPolicyService(MedicalPolicy medicalPolicy) {
        //判断参数
        if (medicalPolicy == null) {
            return new Result(201, "提交的数据不能为空", null);
        }
        medicalPolicy.setCreateTime(new Date());
        medicalPolicy.setUpdateTime(new Date());
        //如果对象不是null  添加操作
        int row = medicalPolicyMapper.add(medicalPolicy);
        if (row != 1) {
            return new Result(202, "添加失败", null);
        }
        return new Result(200, "添加成功", null);
    }

    /*根据id删除数据*/
    @Override
    public Result deleteByIdService(Long id) {
        if (id == null) {
            return new Result(201, "没有需要删除的数据", null);
        }
        //执行删除
        int row = medicalPolicyMapper.deleteById(id);
        if (row != 1) {
            return new Result(202, "删除失败，请重试", null);
        }

        return new Result(200, "删除成功", null);
    }

    @Override
    public Result findAllMedicalPolicyService() {
        //查询数据操作
        List<MedicalPolicy> list = medicalPolicyMapper.findAllMedicalPolicy();
        if (list.isEmpty()) {
            return new Result(201, "没有数据", null);
        }
        return new Result(200, "查询成功", list);
    }

    @Override
    public Result updateMedicalPolicyByIdService(MedicalPolicy medicalPolicy) {
        // 1. 完善参数校验
        if (medicalPolicy == null) {
            return new Result(201, "提交的数据不能为空", null);
        }
        if (medicalPolicy.getId() == null) {
            return new Result(201, "没有指定修改数据的ID", null);
        }

        // 2. 更新修改时间
        medicalPolicy.setUpdateTime(new Date()); // 补充更新时间戳

        // 3. 执行修改操作
        int row = medicalPolicyMapper.updateMedicalPolicyById(medicalPolicy);
        if (row != 1) {
            return new Result(202, "修改失败（可能ID不存在）", null);
        }
        return new Result(200, "修改成功", null);
    }


}
